package com.xbts.web.controller.editor;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.SeiDataDict;
import com.xbts.system.service.ISeiDataDictService;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 数据字典对象Controller
 * @author xbts
 * @date 2023-09-11
 */
@Api("数据字典对象")
@RestController
@RequestMapping("/system/dict")
public class SeiDataDictController extends BaseController {

    @Autowired
    private ISeiDataDictService seiDataDictService;

    /**
     * 查询数据字典对象列表
     */
    @ApiOperation("查询数据字典对象列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SeiDataDict seiDataDict) {
        startPage();
        QueryWrapper<SeiDataDict> queryWrapper = seiDataDict.queryWrapper();
        queryWrapper.eq("parent_id", "0");
        List<SeiDataDict> list = seiDataDictService.list(queryWrapper);
        for (SeiDataDict dataDict : list) {

            List<SeiDataDict> children = seiDataDictService.list(new LambdaQueryWrapper<SeiDataDict>()
                    .eq(SeiDataDict::getParentId, dataDict.getDataDictId()));
            if(children.size() < 1) continue;
            String dictDetails = "";
            for (int i = 1; i <= children.size(); i++) {
                dictDetails += i + "." + children.get(i - 1).getDictName() + ";";
            }
            dataDict.setDictDetails(dictDetails);
        }

        return getDataTable(list);
    }

    /**
     * 查询数据字典所有一级对象列表
     */
    @ApiOperation("查询数据字典对象列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/allType")
    public AjaxResult allType() {
        List<SeiDataDict> list = seiDataDictService.list(new LambdaQueryWrapper<SeiDataDict>()
                .eq(SeiDataDict::getParentId, "0"));
        return success(list);
    }
    /**
     * 查询数据字典所有一级对象列表
     */
    @ApiOperation("查询数据字典对象列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @ApiImplicitParam(name = "parentId", value = "数据字典对象父级ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @GetMapping("/getByParentId/{parentId}")
    public AjaxResult getByParentId(@PathVariable("parentId") String parentId) {
        List<SeiDataDict> list = seiDataDictService.list(new LambdaQueryWrapper<SeiDataDict>()
                .eq(SeiDataDict::getParentId, parentId));
        return success(list);
    }

    /**
     * 导出数据字典对象列表
     */
    @ApiOperation("导出数据字典对象列表")
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @Log(title = "数据字典对象", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody SeiDataDict seiDataDict) {
        List<SeiDataDict> list = seiDataDictService.selectSeiDataDictList(seiDataDict);
        ExcelUtil<SeiDataDict> util = new ExcelUtil<SeiDataDict>(SeiDataDict.class);
        util.exportExcel(response, list, "数据字典对象数据");
    }

    /**
     * 获取数据字典对象详细信息
     */
    @ApiOperation("获取数据字典对象详细信息")
    @ApiImplicitParam(name = "dataDictId", value = "数据字典对象ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dataDictId}")
    public AjaxResult getInfo(@PathVariable("dataDictId") String dataDictId) {
        SeiDataDict dataDict = seiDataDictService.getById(dataDictId);
        List<SeiDataDict> children = seiDataDictService.list(new LambdaQueryWrapper<SeiDataDict>()
                .eq(SeiDataDict::getParentId, dataDict.getDataDictId()));
        dataDict.setChildren(children);
        return success(dataDict);
    }

    /**
     * 新增数据字典对象
     */
    @ApiOperation("新增数据字典对象")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "数据字典对象", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<SeiDataDict> list) {
        if(list.size() < 1){
            return success();
        }
        seiDataDictService.remove(new LambdaQueryWrapper<SeiDataDict>()
                .eq(SeiDataDict::getParentId, list.get(0).getParentId()));
        return toAjax(seiDataDictService.saveBatch(list));
    }

    /**
     * 修改数据字典对象
     */
    @ApiOperation("修改数据字典对象")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "数据字典对象", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SeiDataDict seiDataDict) {
        return toAjax(seiDataDictService.updateSeiDataDict(seiDataDict));
    }

    /**
     * 删除数据字典对象
     */
    @ApiOperation("删除数据字典对象")
    @ApiImplicitParam(name = "dataDictIds", value = "数据字典对象ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "数据字典对象", businessType = BusinessType.DELETE)
	@PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> dataDictIds) {
        return toAjax(seiDataDictService.deleteSeiDataDictByDataDictIds(dataDictIds));
    }
}
